﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WebBotPlugin
{
    public partial class WebBotSetup : Form
    {
        Plugin Plugin { get; set; }

        public WebBotSetup()
        {
            InitializeComponent();
        }

        public WebBotSetup(Plugin plugin)
        {
            InitializeComponent();
            Plugin = plugin;
            Plugin.ReloadSettings();
            txtUserID.Text = Plugin.UserID;
            txtPassword.Text = Plugin.Password;

            if (Plugin.ShowWhispers)
            {
                chkShowWhispers.Checked = true;
            }
            else
            {
                chkShowWhispers.Checked = false;
            }
        }

        public bool IsAlphaNumeric(String str)
        {
            Regex regexAlphaNum = new Regex("[^a-zA-Z0-9-_]");
            return !regexAlphaNum.IsMatch(str);
        }

        private void txtUserID_TextChanged(object sender, EventArgs e)
        {
            if (IsAlphaNumeric(txtUserID.Text) && txtUserID.TextLength > 1)
            {
                lblValid.Text = "Valid UserID";
                lblValid.ForeColor = Color.Green;
            }
            else
            {
                lblValid.Text = "Invalid UserID";
                lblValid.ForeColor = Color.Red;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (checkConfig())
            {
                Plugin.UserID = txtUserID.Text;
                Plugin.Password = txtPassword.Text;

                if (chkShowWhispers.Checked)
                {
                    Plugin.ShowWhispers = true;
                }
                else
                {
                    Plugin.ShowWhispers = false;
                }

                Plugin.SaveSettings();

                Close();
            }
            else
            {
                MessageBox.Show("Make sure your UserID passes validation before saving.");
            }
        }

        private void loadSettings()
        {
            txtUserID.Text = Plugin.UserID;
            txtPassword.Text = Plugin.Password;
        }

        private bool checkConfig()
        {
            if (IsAlphaNumeric(txtUserID.Text) && txtUserID.TextLength > 1)
            {
                return true;
            }

            return false;
        }
    }
}
